import { sys } from 'cc';
import { cloneDeep } from '../tools/data';

export class LocalStore {
    static #instance: LocalStore = null;
    private constructor() {}
    private store: { [key: string]: any };
    public static get instance(): LocalStore {
        if (!LocalStore.#instance) LocalStore.#instance = new LocalStore();
        return LocalStore.#instance;
    }
    public load(key: string) {
        const value = sys.localStorage.getItem(key);
        this.store[key] = value;
    }
    public getByKey<T>(key: string): T {
        return cloneDeep(this.store[key]);
    }
    public setByKey(key: string, value: any) {
        this.store[key] = value;
        sys.localStorage.setItem(key, JSON.stringify(value));
    }
}
